<?php
namespace EApiSwoole\Databases;

use EApiSwoole\Foundation\Application;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
/**
 *
 */
class Pool
{

    /**
     * @var Application
     */
    protected $app;

    protected $config;

    protected $pool;

    public function __construct(Application $app)
    {
        $this->app = $app;
        $this->config = $app->make("config");
        $this->init();
    }

    protected function init()
    {
        $config = (new PDOConfig)
                  ->withHost($this->config->get("database.mysql.host"))
                  ->withPort($this->config->get("database.mysql.port"))
                  ->withDbName($this->config->get("database.mysql.database"))
                  ->withUsername($this->config->get("database.mysql.username"))
                  ->withPassword($this->config->get("database.mysql.password"));
        $this->pool = new PDOPool($config, $this->config->get("database.mysql.pool.size"));
    }


    public function getConnection()
    {
        return $this->pool->get();
    }

    public function put($pdo)
    {
        $this->pool->put($pdo);
    }
	  public function close($connection = null)
	    {
	        $this->pool->put($connection);
	    }
}
